package com.dglg.spring.controller;

import com.dglg.spring.dao.TrainingClassDao;
import com.dglg.spring.model.TeachComment;
import com.dglg.spring.service.CommentService;
import com.dglg.spring.service.impl.CommentServiceImpl;
import com.dglg.spring.service.impl.TrainingClassServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class CommentColler {

    @Autowired
    CommentServiceImpl commentServiceImpl;

    @Autowired
    TrainingClassDao trainingClassDao;

    // 添加评教信息
    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    public Result addComment(@RequestBody Map<String, String> map) {

        TeachComment teachComment = new TeachComment();

        teachComment.setclassName(map.get("className"));   // 将传进来的表单信息封装成类
        teachComment.setStudentId(map.get("studentId"));
        teachComment.setContent(map.get("content"));
        teachComment.setDevice(map.get("device"));

        System.out.println("传进来的表单：" + map);
        System.out.println(teachComment);

        // 添加评教信息
        int count = commentServiceImpl.addComment(teachComment.getStudentId(), teachComment.getclassName(), teachComment.getContent(), teachComment.getDevice());

        // 更新评教信息
        count = trainingClassDao.updateComment(teachComment.getclassName(), teachComment.getStudentId());

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据查询成功！" : "数据查询失败，请重试！";

        return new Result(code, count, msg);
    }
}